package com.tvunetworks.center.device.config.web;

import com.tvunetworks.center.common.exception.NoLoginException;
import com.tvunetworks.center.common.exception.OperationException;
import com.tvunetworks.center.common.exception.ParameterException;
import com.tvunetworks.center.common.exception.ServerException;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
/**
 * @author lebron
 * @ClassName: MyMybatisProperties
 * @Description: Mybatis参数

 */
@ControllerAdvice
@Slf4j
public class GlobalHandlerExceptionResolver extends DefaultHandlerExceptionResolver {
    /**
     *
     * @param e e
     * @return Object
     */
    @ExceptionHandler(ServerException.class)
    @ResponseBody
    public Object resolveServerException(ServerException e) {
        return e.getResult();
    }

    /**
     *
     * @param ex e
     * @return Result
     */
    @ExceptionHandler(value = OperationException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public Result operationHandler(OperationException ex) {
        return ResultUtil.error(ex.getErrorCode(), ex.getInfo());
    }

    /**
     *
     * @param ex e
     * @return Object
     */
    @ExceptionHandler(Throwable.class)
    @ResponseBody
    public Object resolveException(Throwable ex) {
        log.error("unhandled exception", ex);
        return ResultUtil.exception();
    }

    /**
     * 对未指定的异常进行处理，默认的异常处理
     *
     * @param req req
     * @param e e
     * @param response e
     * @return Result
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result defaultExceptionHandler(HttpServletRequest req, HttpServletResponse response, Exception e) {
        log.error(e.getMessage(), e);

        ModelAndView modelAndView = doResolveException(req, response, null, e);
        if(modelAndView == null) {
            Map<String, Object> info = new HashMap<>();
            info.put("url", req.getRequestURL());
            info.put("param", req.getParameterMap());
            info.put("message", e.getMessage());
            return ResultUtil.exception(info);
        }
        // 实际上有默认的格式  CustomerErrorAttributes
        return null;

    }

    /**
     * 未登陆异常，有时可能出现zuul中显示用户已经登陆，但是到内部服务时用户却没有登陆的情况
     *
     * @param e e
     * @return Result
     */
    @ExceptionHandler(value = NoLoginException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Result noLoginHandler(NoLoginException e) {
        return ResultUtil.noLogin(e.getMessage());
    }

    /**
     * 对参数异常进行处理，包括 hibernate 的参数校验异常和cc自定义的参数异常
     * <p>
     * BindException 是 hibernate 的参数校验异常
     *
     * @param e e
     * @return Result
     * @throws Exception 编译异常
     */
    @ExceptionHandler(value = {ParameterException.class, BindException.class})
    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST) // 设置状态码为400
    public Result validateParametersErrorHandler(Exception e) {

        if (e instanceof BindException) {
            return ResultUtil.paramErrorInfo("Parameter error");
        }

        return ResultUtil.paramErrorInfo(e.getMessage());
    }


}
